package com.example.forum.controller;


import com.example.forum.common.AppConfig;
import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.services.IArticleService;
import com.example.forum.services.IBoradService;
import com.example.forum.utils.StringUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private IBoradService iBoradService;

    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,@NonNull String title,
                            @NonNull Long boardId,@NonNull String content){
        //查看用户是否被禁言
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            //用户被禁言了
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }

        Article article = new Article();
        article.setUserId(user.getId());
        article.setTitle(title);
        article.setContent(content);
        article.setBoardId(boardId);
        iArticleService.create(article);
        return AppResult.success();

    }

    @RequestMapping("/getAllByBoardId")
    public AppResult<List> getAllByBoardId(@RequestParam(value = "boardId",required = false)Long boardId){
        List<Article> articles;
        if(boardId==null){
            //没有传入boardId查询全部的发帖信息
             articles = iArticleService.selectAll();
            if(articles==null){
                articles=new ArrayList<>();
            }
        }else {
            //查询板块id的发帖信息
            articles = iArticleService.selectByBoardId(boardId);
            if(articles==null){
                articles=new ArrayList<>();
            }
        }

        return AppResult.success(articles);
    }

    @GetMapping("/details")
    public AppResult<Article> getDetails(HttpServletRequest request ,@NonNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);

        Article article = iArticleService.selectDetailById(id);
        if(article==null){
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_NOT_EXISTS));
        }
        //判断当前文章的用户id和登录者的用户id是否相同
        if(article.getUserId()==user.getId()){
            article.setOwn(true);
        }
        return AppResult.success(article);
    }

    @PostMapping("/modify")
    public AppResult modify(HttpServletRequest request ,
                            @NonNull Long id,
                            @NonNull String title,
                            @NonNull String content){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否被禁言
        if(user.getState()!=0){
            log.warn("用户被禁言了");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //获取帖子原来的数据
        Article article = iArticleService.selectById(id);
        if(article==null){
            log.warn("帖子不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //发帖人不是当前用户
        if(article.getUserId()!= user.getId()){
            log.warn("发帖人不是当前用户");
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //帖子状态异常
        if(article.getState()!=0||article.getDeleteState()!=0){
            log.warn("帖子状态异常");
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        iArticleService.modify(id,title,content);
        return AppResult.success();

    }

    @PostMapping("/thumbsUp")
    public AppResult thumbsUp(HttpServletRequest request,@NonNull Long id){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //判断用户是否被禁言
        if(user.getState()!=0){
            log.warn("用户被禁言了");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        iArticleService.thumbsUpById(id);
        return AppResult.success();
    }

    @PostMapping("/delete")
    public AppResult deleteById(HttpServletRequest request ,@NonNull Long id){

        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        //查看用户是否被禁言
        if(user.getState()==1){
            log.info(ResultCode.FAILED_USER_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //查看帖子是否存在
        Article article = iArticleService.selectById(id);
        if(article==null||article.getDeleteState()==1){
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        //用户是否是作者
        if(article.getUserId()!=user.getId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        iArticleService.deleteById(id);
        return AppResult.success();

    }

    @GetMapping("/getAllByUserId")

    public AppResult<List<Article>> getAllByUserId(HttpServletRequest request ,@RequestParam(value = "userId", required = false) Long userId){

        if(userId==null){
            HttpSession session = request.getSession(false);
            User user = (User)session.getAttribute(AppConfig.USER_SESSION);
            userId=user.getId();
        }
        List<Article> articles = iArticleService.selectByUserId(userId);

        return AppResult.success(articles);
    }



}
